/**
 * File: Catalog.java
 * Author: Ben Stutsky
 * Team: Bob-omb Squad
 * Date: 4/18/11
 * Email: stutsky1@umbc.edu
 */
package clientApp;

import java.util.ArrayList;

/**
 * The Class Catalog.
 */
public class Catalog 
{
	
	/** The courses. */
	private ArrayList<Course> courses = new ArrayList<Course>();
	
	/**
	 * Instantiates a new catalog.
	 */
	public Catalog()
	{
		//init without loading courses
		//for testing purposes, remove before demo
	}
	
	/**
	 * Instantiates a new catalog.
	 *
	 * @param myCourses the my courses
	 */
	public Catalog(ArrayList<Course> myCourses)
	{
		setCourses(myCourses);
	}
	
	public boolean changeCourseID(String courseID, String newCourseID)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.setCourseCode(newCourseID);
		
		return true;
	}
	
	public boolean editCourseName(String courseID, String courseName)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.setCourseName(courseName);
		
		return true;
	}
	
	public boolean editCourseDescription(String courseID, String courseDesc)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.setDescription(courseDesc);
		
		return true;
	}
	
	public boolean removeCoursePrereq(String courseID, String coursePrereq)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.removePrereq(coursePrereq);
		
		return true;
	}
	
	public boolean addCoursePrereq(String courseID, String coursePrereq)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.addPrereq(coursePrereq);
		
		return true;
	}
	
	public boolean addAvailability(String courseID, int season)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.addAvailability(season);
		
		return true;
	}
	
	public boolean removeAvailability(String courseID, int season)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.removeAvailability(season);
		
		return true;
	}
	
	public boolean editNumCredits(String courseID, int creds)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return false;
		}
		
		temp.setNumCreds(creds);
		
		return true;
	}
	
	public double getNumCredits(String courseID) {
		Course temp = getShallowCourse(courseID);
		if (temp == null) {
			return -1;
		} else {
			return temp.getNumCreds();
		}
	}
	
	/**
	 * Gets all the courses.
	 *
	 * @return the courses
	 */
	public ArrayList<Course> getCourses() 
	{
		ArrayList<Course> myCourses = new ArrayList<Course>();
		for(int i = 0; i != courses.size(); i++)
		{
			myCourses.add(new Course(courses.get(i)));
		}
		return myCourses;
	}

	/**
	 * Sets the courses. Will overwrite all previously added courses.
	 * This is usually for first entry of Courses into the catalog.
	 *
	 * @param courses the new courses
	 */
	private void setCourses(ArrayList<Course> myCourses) 
	{
		courses = new ArrayList<Course>();
		for(int i = 0; i != myCourses.size(); i++)
		{
			courses.add(new Course(myCourses.get(i)));
		}
	}
	
	/**
	 * Gets a specific course.
	 *
	 * @param courseID the course id
	 * @return the course
	 */
	public Course getCourse(String courseID)
	{
		Course temp = getShallowCourse(courseID);
		if(temp == null)
		{
			return null;
		}
		return new Course(temp);
	}
		
	public boolean deleteCourse(String courseCode)
	{
		for(int i = 0; i != courses.size(); i++)
		{
			Course tempCourse = courses.get(i);
			if(tempCourse.getCourseCode().compareTo(courseCode) == 0)
			{
				courses.remove(i);
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Adds the course.
	 *
	 * @param newCourse the new course
	 */
	public void addCourse(Course newCourse)
	{
		courses.add(new Course(newCourse));
	}
	
	private Course getShallowCourse(String courseID)
	{
		for(int i = 0; i != courses.size(); i++)
		{
			Course temp = courses.get(i);
			if(temp.getCourseCode().compareTo(courseID) == 0)
			{
				return temp;
			}
		}
		return null;
	}
	
	public static void main(String[] args)
	{
		Catalog myCatalog = new Catalog();
		ArrayList<Course> myCourses = new ArrayList<Course>();
		String[] pre = new String[2];
		pre[0] = "CMSC104";
		pre[1] = "INTROA";
		int[] avail = new int[2];
		avail[0] = ProjectConstants.FALL;
		avail[1] = ProjectConstants.SPRING;
		myCourses.add(new Course("CMSC201", "Some course", "desc", pre, avail, 4));
		myCourses.add(new Course("CMSC202", "Some course", "desc", pre, avail, 4));
		myCourses.add(new Course("CMSC203", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSC313", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSC341", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSC331", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSC411", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSC421", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSCmeh", "Some course", "desc", pre, avail, 4));
		myCourses.add(new Course("CMSCsomething", "Some course", "desc", pre, avail, 4));
		myCourses.add(new Course("CMSCother", "Some course", "desc", pre, avail, 3));
		myCourses.add(new Course("CMSCyeah", "Some course", "desc", pre, avail, 4));
		
		
		myCatalog = new Catalog(myCourses);
		
		System.out.println("Printing Catalog:\n");
		ArrayList<Course> courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest change courseID\n");
		myCatalog.changeCourseID("CMSC201", "CMSC102");
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest change course name\n");
		myCatalog.editCourseName("CMSC202", "THE COURSE");
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest change course dec\n");
		myCatalog.editCourseDescription("CMSC203", "THE DESC");
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest remove prereq\n");
		myCatalog.removeCoursePrereq("CMSCyeah", "INTROA");
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest add prereq\n");
		myCatalog.addCoursePrereq("CMSCyeah", "MAGIC");
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest add avail\n");
		myCatalog.addAvailability("CMSCyeah", ProjectConstants.SUMMER);
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\nTest remove avail\n");
		myCatalog.removeAvailability("CMSCyeah", ProjectConstants.FALL);
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\ntest edit num cred\n");
		myCatalog.editNumCredits("CMSCyeah", 2);
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\ntest delete course\n");
		myCatalog.deleteCourse("CMSCyeah");
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
		
		System.out.println("\ntest add course\n");
		myCatalog.addCourse(new Course("CMSCyeah", "Some course", "desc", pre, avail, 4));
		courses = myCatalog.getCourses();
		for(Course course : courses)
		{
			System.out.println(course);
		}
	}
}
